package com.cloud.mkz.core.common.util;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 加密解密工具类
 *
 * @author kuangjianyu
 * @date 2021-01-26
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class JasyptUtils {
    private final StringEncryptor encryptor;

    /**
     * 加密
     * @param string
     * @return
     */
    public String getEncryptResult(String string) {
        String encryptResult = encryptor.encrypt(string);
        log.info("原字符串：{} ,加密后字符串: {}",string,encryptResult);
        return encryptResult;
    }

    /**
     * 解密
     * @param string
     * @return
     */
    public String getDecryptResult(String string) {
        String decryptResult = encryptor.decrypt(string);
        log.info("加密后字符串：{} ,原字符串: {}",string,decryptResult);
        return decryptResult;
    }

    public static void main(String[] args) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //相当于密钥加盐，这个要和配置文件一致
        encryptor.setPassword("MkzCloudJasyptSalt");
        String string="mkz_config";
        String encryptResult = encryptor.encrypt(string);
        log.info("原字符串：{} ,加密后字符串: {}",string,encryptResult);
        String string1="config_auth";
        String encryptResult1 = encryptor.encrypt(string1);
        log.info("原字符串：{} ,加密后字符串: {}",string1,encryptResult1);

        String string2="Yem4kKtAeKnmaAFeyCg3Pg==";
        String decryptResult = encryptor.decrypt(string2);
        log.info("加密后字符串：{} ,原字符串: {}",string2,decryptResult);
    }
}